package com.zjpavt.client.udp;

import com.zjpavt.client.enums.ExtendDeviceEnum;
import com.zjpavt.client.hal.ExtendDeviceManager;
import com.zjpavt.client.nativemethod.JnIMethod;
import com.zjpavt.client.util.ClientConfig;
import com.zjpavt.client.util.LoggerUtil;
import com.zjpavt.client.util.PropertiesUtil;
import com.zjpavt.client.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;

/**
 * 用于向UDP端口发送数据，暂时只是接口写上，在设备中暂时未应用
 * @author zyc
 */
@Slf4j
public class UdpSender {

    private static final Logger logger = LoggerFactory.getLogger(UdpSender.class);

    /**
     * 发送UDP包到默认端口
     *
     * @param bytes
     * @return
     */
    public static boolean sendUdpPacket(byte[] bytes) {
        return sendUdpPacket(ClientConfig.OUTPUT_UDP_SERVER_IP, ClientConfig.OUTPUT_UDP_SERVER_PORT, bytes);
    }

    /**
     * 发送suite场景。
     * 从文件中获取suite设备的IP地址 如果获取不到则使用默认的IP=192.168.1.220
     * 可以编辑suite_device_ip,指定UDP地址 第四位地址可以设置到255发UDP广播。
     * @param sceneId
     * @return
     */
    public static boolean sendSuiteScene(int sceneId) {
        long startTime = System.currentTimeMillis();
        logger.info("sendSuiteScene SCENE_ID =" + sceneId);
        String suiteIp = null;
        try {
            suiteIp = PropertiesUtil.getPropertiesDefault("suite_device_ip");
        } catch (IOException e) {
            logger.info(LoggerUtil.convertException2String(e));
        }
        String ip = StringUtil.checkStringNotEmptyAndNull(suiteIp,"192.168.27.255");
        String s = "53697564695f37426d000" + sceneId + "000000010000000000";
        byte[] bytes = new byte[s.length() / 2];
        for (int i = 0; i < s.length() / 2; i++) {
            bytes[i] = (byte) (Character.digit(s.charAt(2 * i), 16) * 16
                               + Character.digit(s.charAt(2 * i + 1), 16));
        }
        sendUdpPacket(ip, 2430, bytes);
        logger.info("ip = " + ip + " SCENE_ID= " + sceneId+ " msg=" + s + " use time=" +(System.currentTimeMillis() -startTime));
//		为了加速 优先发送udp指令 然后切换suite信号源
        try {
            if (ExtendDeviceManager.getInstance().containsExtendDevice(ExtendDeviceEnum.DMX_EXCHANGE)) {
//				JnIMethod.changeDmxInputMouth('B');
            }
        } catch (Exception e) {
            logger.error(LoggerUtil.convertException2String(e));
        }
        return true;
    }

    public static boolean sendUdpPacket(String hostName, int port, byte[] bytes) {
        InetSocketAddress targetHost = new InetSocketAddress(hostName, port);
        // 发送udp内容
        DatagramSocket socket = null;
        try {
            socket = new DatagramSocket();
            socket.send(new DatagramPacket(bytes, 0, bytes.length, targetHost));
            return true;
        } catch (IOException e) {
            log.error(LoggerUtil.convertException2String(e));
        }
        logger.error("fail to send udp packet");
        return false;
    }

    public static void main(String[] args) {
        /*byte[] controlByte = {0x41, 0x72, 0x74, 0x2D, 0x4E, 0x65, 0x74, 0x00, 0x00, 0x50, 0x00, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00};
        for (int i = 0; i < ; i++) {
        }*/
    }
}
